#include <stdio.h>
#include <string.h>
#include <unistd.h> // 包含 sysconf 函数的头文件

int main()
{
    // 系统配置的CPU数量
    long cpu_num = sysconf(_SC_NPROCESSORS_CONF);
    printf("cpu数量:%ld\n", cpu_num);

    // 读取 /proc/stat 文件
    FILE* fp = fopen("/proc/stat", "r");
    if (fp == NULL)
    {
        perror("fopen");
        return 1;
    }

    // 读取 CPU 时间信息
    char line[256];
    long user, nice, system, idle, iowait, irq, softirq, steal, guest, guest_nice;
    int ret;
    while (1) {
        if (fgets(line, sizeof(line), fp) == NULL) {
            break;
        }
        if (strstr(line, "cpu"))
        {
            ret = sscanf(line, "%*s %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld", &user, &nice, &system, &idle, &iowait, &irq, &softirq, &steal, &guest, &guest_nice);
            if (ret > 0)
            {
                printf("ut:%ld ni:%ld sy:%ld id:%ld io:%ld ir:%ld so:%ld st:%ld gu:%ld gni:%ld\n",
                    user, nice, system, idle, iowait, irq, softirq, steal, guest, guest_nice);
            }
        }
    }

    return 0;
}